1. Field of the Invention
The present invention relates to objects defined within an object-oriented computer programming system. More specifically, the present invention relates to a method and apparatus for storing short-lived objects within an object-oriented computer programming system.
2. Related Art
The recent proliferation of ever smaller and more capable computing devices has lead to the use of platform-independent programming languages on these smaller devices. Platform-independent programming languages facilitate the creation of programs that can be compiled to a set of platform-independent codes, which can be executed on a variety of computing devices. Many of these computing devices have a virtual machine (VM) containing an interpreter or just-in-time compiler to execute these platform-independent codes. The JAVA(trademark) programming language is an example of a platform-independent programming language and JAVA bytecodes are an example of platform-independent codes.
The terms JAVA, JVM and JAVA VIRTUAL MACHINE are registered trademarks of SUN Microsystems, Inc. of Palo Alto, Calif.
Many VMs use an area of memory called a heap to store various objects. When an object in the heap is no longer needed, all references pointing to the object will have been deleted. However, the object continues to occupy space in the heap. In some VMs, a computer program called a garbage collector scans the heap and deletes objects that are not being referenced in order to reclaim unused space in the heap.
There are many implementations of garbage collectors, however, they all share some traits. Garbage collectors require resources of the computing device in order to operate. This uses storage space in the computing device and slows execution times of running programs. There have been many attempts to reduce the impact of garbage collection on executing programs. For instance, a garbage collector can be run in a separate execution thread. If the computing device has more than one processor, the separate execution thread can run on another processor, thereby reducing the impact on the main execution thread.
Another attempt to reduce the impact of garbage collection involves the use of so called xe2x80x9cgenerational garbage collectors.xe2x80x9d These work by migrating long-lived objects to an area of the heap where garbage collection is performed less often and leaving short-lived objects in an area of the heap where garbage collection is performed more often and possibly performed using a different method.
Neither of these techniques, however, is well suited for small computing devices. Small computing devices have only one processor, therefore running a separate garbage collection thread causes the main program thread to be xe2x80x9cswapped outxe2x80x9d during garbage collection. Furthermore, generational garbage collectors require large amounts of storage space, which may not be available on a small computing device.
Moreover, many VMs generate a large number of objects that will remain active for only a very short period of time. For example, the JAVA runtime environment creates a large number of java.lang.StringBuffer objects, which remain available for only several instructions or, at most, during the lifetime of a single function of a program. These objects occupy a considerable portion of the normal heap and, as a result, cause the garbage collector to consume a considerable amount of processor time, even though these objects never remain in the heap for more than one cycle of the garbage collector.
As an example of how these short-lived objects are created, consider the following JAVA source code.
class StringBufferTst {
Object x;
public static void main(String[ ] args) {
String s1=xe2x80x9cHello xe2x80x9c;
String s2=xe2x80x9cStringBufferxe2x80x9d;
System.out.println(s1+s2);
}
}
When compiled by a JAVA compiler, the JAVA source code is translated into the following series of bytecodes.
Method void main(java.lang.String[ ])
0 ldc #2  less than String xe2x80x9cHello xe2x80x9d greater than 
2 astore_1
3 ldc #3  less than String xe2x80x9cStringBufferxe2x80x9d greater than 
5 astore_2
6 getstatic #4  less than Field java.io.Printstream out greater than 
9 new #5  less than Class java.lang.StringBuffer greater than 
12 dup
13 invokespecial #6  less than Method java.lang.StringBuffer( ) greater than 
16 aload_1
17 invokevirtual #7  less than Method java.lang.StringBuffer
append(java.lang.String) greater than 
20 aload_2
21 invokevirtual #7  less than Method java.lang.StringBuffer
append(java.lang.String) greater than 
24 invokevirtual #8  less than Method java.lang.String toString( ) greater than 
27 invokevirtual #9  less than Method void
println(java.lang.String) greater than 
30 return
Note that the object created by bytecode number 9 is short-lived. In fact, this object has no useful life after bytecode number 27.
What is needed is a system that eliminates the waste of resources caused by the garbage collector operating on objects that are usually deleted shortly after being created.
One embodiment of the present invention provides a system for storing short-lived objects defined within an object-oriented programming system. These short-lived objects are created in a virtual machine used for executing platform-independent code and are ordinarily created during normal operation of the virtual machine. The system works by allocating a storage area reserved for short-lived objects that uses a method of garbage collection optimized for short-lived objects. After the storage area is allocated, the system receives requests to create an object. The system then determines if the object is a short-lived object by referring to a table of short-lived objects. If the object is a short-lived object, it is created and placed in the reserved storage area.
In one embodiment of the present invention, the reserved storage area is an area of a heap used by the virtual machine.
In one embodiment of the present invention, the remainder of the heap is subject to garbage collection using a method that is optimized for objects of ordinary life span.
In one embodiment of the present invention, the short-lived object is a transient string buffer object.
In one embodiment of the present invention, the transient string buffer object is a java.lang.StringBuffer object.
In one embodiment of the present invention, the storage area is organized into an array of objects.
In one embodiment of the present invention, an element of the array is sized to accommodate a largest short-lived object within the virtual machine.
In one embodiment of the present invention, the virtual machine is a JAVA VIRTUAL MACHINE.